﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using IFDB.Models.Contexts;
using IFDB.Models.Tables;
using IFDB.ResourceLibrary;
using IFDB.ViewModels.PartialViewModels.Interfaces;

namespace IFDB.ViewModels.PartialViewModels
{
    public class DaysMatchesViewModel : IDaysMatchesViewModel
    {
        public DaysMatchesViewModel(DateTime viewDate)
        {
            this.ViewDate = viewDate;
        }

        public DateTime ViewDate { get; set; }

        public IEnumerable<Match> DaysMatches
        {
            get
            {
                DateTime startDate = ViewDate.StartOfDay();
                DateTime endDate = ViewDate.EndOfDay();

                using (IFDBContext db = new IFDBContext())
                    return (from m in db.Matches
                            .Include(m => m.Team1.TeamNames)
                            .Include(m => m.Team2.TeamNames)
                            where (m.MatchDate >= startDate && m.MatchDate <= endDate)
                            && (m.MatchStatus == MatchStatus.ScoreOnly || m.MatchStatus == MatchStatus.FullMatch)
                            orderby m.Team1.TeamNames.First()
                            select m).ToList();
            }
        }
    }
}
